package com.ynet.middleground.user.service.impl;

import javax.validation.Valid;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.ynet.middleground.user.bean.QueryBlacListInfoReq;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.BlacListInfoDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.OperationBlacListReq;
import com.ynet.middleground.user.model.BlackListManagementModel;
import com.ynet.middleground.user.service.BlackListManagementService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * @program: 2019-10-12_userCenter
 * @description: 黑白名单管理服务类
 * @author: DaiGaoLe
 * @create: 2019-10-30 16:45
 **/
@Path("blacList")
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
@Api(value = "黑白名单管理服务类", tags = {"黑白名单管理服务类"})
@Service(timeout = 6000, version = "0.0.1")
public class BlackListManagementServiceImpl implements BlackListManagementService, GenericService {

    @Autowired
    BlackListManagementModel blacListManagementModel;

    /* 新增黑白名单 begin */
    @POST
    @Path("add")
    @ApiOperation(value = "新增黑白名单", notes = "新增黑白名单，用户中心提供黑名单维护服务，具体如何使用渠道自行判断。")
    @Override
    public ServiceResult<String> addBlacList(OperationBlacListReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            result = blacListManagementModel.addBlacList(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;

    }
    /* 新增黑白名单 end */

    /* 移除黑白名单 begin */
    @POST
    @Path("remove")
    @ApiOperation(value = "移除黑白名单", notes = "移除黑白名单，内管无特殊授权配置，哪个渠道添加的黑名单由哪个渠道删除。")
    @Override
    public ServiceResult<String> removeBlacList(OperationBlacListReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            result = blacListManagementModel.removeBlacList(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                    Throwables.getStackTraceAsString(e));
            resultService.setError("00001", ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setResult(result);
        return resultService;

    }
    /* 移除黑白名单 end */


    /* 黑白名单查看 begin */
    @POST
    @Path("query")
    @ApiOperation(value = "黑白名单查看", notes = "黑白名单查看，可以查询全部渠道黑名单数据。")
    @Override
    public ServiceResult<List<BlacListInfoDto>> queryBlacList(@Valid QueryBlacListInfoReq requestObject) {
        ServiceResult<List<BlacListInfoDto>> resultService = new ServiceResult<>();
        List<BlacListInfoDto> blacListInfoDtos = null;
        try {
            blacListInfoDtos = blacListManagementModel.queryBlacList(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            resultService.setError("00001", ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        resultService.setPagerInfo(requestObject.getPagerInfo());
        resultService.setResult(blacListInfoDtos);
        return resultService;
    }
    /* 黑白名单查看 end */
}
